package com.hsytech.dijiebao.expand.rongclound;

import java.util.Locale;

import org.json.JSONArray;
import org.json.JSONObject;

import com.hsytech.dijiebao.R;
import com.hsytech.dijiebao.base.BaseActivity;
import com.hsytech.dijiebao.utils.Constants;
import com.hsytech.dijiebao.utils.SharedPreferencesTools;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import io.rong.imkit.fragment.ConversationFragment;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.UserInfo;

public class ConversationActivity extends BaseActivity {
	private ImageView bar_right_top_btn;
	private UserInfo uf;
	private ImageButton top_back_btn;
	private Uri uri;
	private String title, rightPhoneNumber;
	private TextView bar_title_tv;
	/**
	 * Ŀ�� Id
	 */
	private String mTargetId;

	/**
	 * �ոմ���������������������id ΪtargetIds����Ҫ���� ΪtargetIds ��ȡ targetId
	 */
	private String mTargetIds;

	/**
	 * �Ự����
	 */
	private Conversation.ConversationType mConversationType;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.conversion);
		Intent intent = getIntent();
		getIntentDate(intent);
		initTitle();
		getHttp();
	}

	private void getHttp() {
//		RequestParams data = new RequestParams();
//		// data.put("city_ids",
//		data.put("uids", mTargetId);
//		Log.d("mylog", "Ŀ��id"+mTargetId);
//		data.put("token", SharedPreferencesTools.getTOKEN(this));
//		HttpUtils.get(Constants.HTTP_CHART, this, data, new JsonResponseHandler() {
//
//			@Override
//			public void onSuccess(JSONObject data) {
//				JSONArray result = data.optJSONArray("result");
//				UserInfo uf = null;
//				for (int i = 0; i < result.length(); i++) {
//					String id = result.optJSONObject(i).optString("id");
//
//					String photoNumber = result.optJSONObject(i).optString("mobile");
//
//					if (id.equals(mTargetId)) {
//						Log.d("mylog", "id=mTargetId" + id + "=" + mTargetId);
//						rightPhoneNumber = photoNumber;
//						Log.d("mylog", rightPhoneNumber);
//					}
//
//				}
//			}
//		});

	}

	private void initTitle() {
		bar_title_tv = (TextView) findViewById(R.id.bar_title_tv);
		bar_title_tv.setText(title);
		bar_right_top_btn = (ImageView) findViewById(R.id.bar_right_top_btn);
		bar_right_top_btn.setOnClickListener(new OnClickListener() {

			@TargetApi(Build.VERSION_CODES.M)
			@Override
			public void onClick(View arg0) {
				Log.d("mylog", "id=mTargetId" + rightPhoneNumber + "=" + mTargetId);
				Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + rightPhoneNumber));
				// ��ʼ��ͼ(������绰)
				if (checkSelfPermission(Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
					// TODO: Consider calling
					//    Activity#requestPermissions
					// here to request the missing permissions, and then overriding
					//   public void onRequestPermissionsResult(int requestCode, String[] permissions,
					//                                          int[] grantResults)
					// to handle the case where the user grants the permission. See the documentation
					// for Activity#requestPermissions for more details.
					return;
				}
				startActivity(intent);
			}
		});
		// bar_right_top_btn.setVisibility(View.GONE);
		top_back_btn = (ImageButton) findViewById(R.id.top_back_btn);
		top_back_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
	}

	/**
	 * չʾ��δ� Intent �еõ� ���ƻỰҳ�洫�ݵ� Uri
	 */
	private void getIntentDate(Intent intent) {

		mTargetId = intent.getData().getQueryParameter("targetId");
		mTargetIds = intent.getData().getQueryParameter("targetIds");
		uri = intent.getData();
		title = uri.getQueryParameter("title").toString();
		// intent.getData().getLastPathSegment();//��õ�ǰ�Ự����
		mConversationType = Conversation.ConversationType
				.valueOf(intent.getData().getLastPathSegment().toUpperCase(Locale.getDefault()));

		enterFragment(mConversationType, mTargetId);
	}

	/**
	 * ���ػỰҳ�� ConversationFragment
	 *
	 * @param mConversationType
	 *            �Ự����
	 * @param mTargetId
	 *            Ŀ�� Id
	 */
	private void enterFragment(Conversation.ConversationType mConversationType, String mTargetId) {

		ConversationFragment fragment = (ConversationFragment) getSupportFragmentManager()
				.findFragmentById(R.id.conversation);

		Uri uri = Uri.parse("rong://" + getApplicationInfo().packageName).buildUpon().appendPath("conversation")
				.appendPath(mConversationType.getName().toLowerCase()).appendQueryParameter("targetId", mTargetId)
				.build();

		fragment.setUri(uri);
	}

}
